//this file contains some classes that provide basicUI reactions
//and do some basic works or some generating HTMLs work

/*class Logger
 *
 *usage: handle tha basic loggerRequest and give
 *a basic UI reaction
 *
 *
 *
 *since version: 0.5
 *
 */

//instance
var logger = null;
function Logger()
{
    this.noShowMessage;
}
Logger.prototype.login = function(username,password,noShowMessage)
{
    if(userManager.login(username,password))
    {
	if(!this.noShowMessage)
	    messageBox.show("成功登录");
	return true;
    }
    else
    {
	if(!this.noShowMessage)
	{
	    messageBox.show("登录失败，5秒后重试");
	}
	setTimeout(function(){This.login(username,password)},5000);
	return false;
    }
}
Logger.prototype.logout = function(username,password,noShowMessage)
{
    var This = this;
    if(userManager.logout())
    {
	if(!noShowMessage)
	{
	    defaultPage.manageStatus();
	    messageBox.show("成功登出");
	    bookLib.showSearchPage();
	}
	return true;
    }
    else
    {
	if(!noShowMessage)
	{
	    messageBox.show("登出失败，稍候将重试");
	}
	return false;
    }
}
Logger.prototype.regist = function(username,password,mailbox,realname)
{
    if(!this.checkRegistValid(username,password,mailbox,realname))
    {
	return false;
    }
    var result = getURL("api/add_client.php?"
			+"username="+username
		       +"&password="+password
		       +"&mailbox="+mailbox
		       +"&realname="+realname);
    if("OK"==result)
    {
	Show("成功注册");
	return true;
    }
    else
    {
	Show("注册失败");
	return false;
    }
}
Logger.prototype.checkRegistValid = function(username,password,mailbox,realname)
{
    if(username.length<3)
	return false;
    if(password.length<6)
    {
	Show("密码太短了哥么");
	return false;
    }
    if(password.length>32)
    {
	Show("密码太长了个么");
	return false;
    }
    return true;
}
/*class searcher
 *
 *to responde to the search request and fill the html
 *
 *
 *
 *since version: 0.5
 *
 */
//instance 
var searcher = null;
function Searcher()
{
    this.resultPerPage = 10;
    this.body = new DOMObject("body");
    this.innerHTML;
}
Searcher.prototype.applySearch = function(keyword)
{
    //total sum of the book
    var num = parseInt(bookManager.searchBooksNum());
    if(isNaN(num))
    {
	Show("搜索错误,请联系管理员");
	return false;
    }
    this.applySearchDiv(keyword,1,num);
    return true;
}
Searcher.prototype.applySearchDiv = function(keyword,currentPage,totalNum)
{    
    var html = this.__genSearchResultHTML(keyword,totalNum,currentPage);
    if(html==null)
    {
	searchPage.show();
	return false;
    }
    this.body.$().innerHTML = html;
    var actions = document.getElementsByClassName("is_available");
    for(var i=0;i<actions.length;i++)
    {
	actions[i].onclick = this.__checkoutFactory(actions[i].id);
    }
    actions = document.getElementsByClassName("return_this");
    for(var i;i<actions.length;i++)
    {
	actions[i].onclick = this.__returnFactory(actions[i].id);
    }
}
Searcher.prototype.__checkoutFactory = function(ID)
{
    return function(){
	librarian.checkoutBook(ID);
	searchPage.show();
    };
}
Searcher.prototype.__returnFactory = function(ID)
{
    return function(){
	librarian.returnBook(ID);
	searchPage.show();
    };
}
Searcher.prototype.__genSearchResultHTML = function(keyword,num,currentPage)
{
    //generateCurentPages
    var html = "";
    var jsonBooks = bookManager.searchBooks(keyword,currentPage*this.resultPerPage-this.resultPerPage+1,currentPage*this.resultPerPage);
    if(jsonBooks==null)
    {
	Show("没有搜索到该关键词的书");
	return null;
    }
    
    for(var i = 0;i<this.resultPerPage&&jsonBooks[i]!=null;i++)
    {
	var result = this.genBookListItem(jsonBooks[i]);
	if(result)
	{
	    html+=result;
	}
    }
    html = WrapTag("div",null,"searchList",null,html);
    //generateIndex
    html += this.__genSearchListIndex(currentPage,num,keyword);
    return html;
    
}
Searcher.prototype.genBookListItem = function(jsonBook)
{
    //this function is unreadable and complex,
    //to see what it generates,see the searchListPage.html
    //div which inherit class listItem;
    var html = "<div class='listItem notFirst'> <div class='upperTab'> <div class='left'> <div class='right'> <div class='center'> <h1 class='bookName'>somename</h1> </div> </div> </div> </div> <div class='upDownExpandBody largeRoundBody'> <div class='up'> <div class='down'> <div class='center'> <div class='listContent'> <span class='attrName'>bookCode:</span> <span>something</span> <span class='attrName'>bookLocation:</span> <span>somewhere</span> <span class='attrName'>bookPublisher:</span> <span>somebody</span> <div class='actions'> <div class='leftRightExpandBody orangeButton clickable is_available' id='someID'><div class='left'><div class='right'><div class='center'> <span>Return Me!</span> </div></div></div></div> </div> <!-- end actions --> </div> <!-- end listContent --> </div> </div> </div> </div> </div>"; 
    html = html.replace("somename",jsonBook.name);
    html = html.replace("something",jsonBook.ID);
    html = html.replace("somewhere",jsonBook.location);
    html = html.replace("somebody",jsonBook.publisher);
    html = html.replace("someID",jsonBook.ID);
    if(jsonBook.is_available.toString()=="1")
    {
	html = html.replace("Return Me!","Check Me Out!");
    }
    else
    {
	html = html.replace("is_available","not_available");
	
	if(jsonBook.username == userManager.username)
	{
	    html = html.replace("is_available","return_this");
  	    
	    return html;
	}
	html = html.replace("Return Me!","Not Available");

    }
    return html;
}
Searcher.prototype.__genSearchListIndex = function(currentPage,totalSum,keyword)
{
    var strNum = " <div class='clickable' onclick='searcher.applySearchDiv(keyword,currentPage,totalSum)'> indexNum </div>";
    var strPrev = "<div class='prev clickable' onclick='searcher.applySearchDiv(keyword,currentPage,totalSum)'> prev </div>";
    var strNext = "<div class='next clickable' onclick=' searcher.applySearchDiv(keyword,currentPage,totalSum)'> next </div>";
    var html = "";
    var temp = "";
    if(currentPage>1)
    {
	temp = strPrev.replace("keyword","\\'"+keyword+"\\'");
	temp = temp.replace("totalSum",totalSum);
	temp = temp.replace("curentPage",currentPage-1);
	html +=temp;
    }
    for(var i=0;i<totalSum/this.resultPerPage;i++)
    {	
	temp = "";
	temp = strNum.replace("keyword","\\'"+keyword+"\\'");
	temp = temp.replace("totalSum",totalSum);
	temp = temp.replace("curentPage",i+1);	
	html+= temp;
    }
    if(currentPage<totalSum/this.resultPerPage)
    {
	temp = "";
	temp = strNext;
	temp = temp.replace("keyword","\\'"+keyword+"\\'");
	temp = temp.replace("totalSum",totalSum);
	temp = temp.replace("curentPage",currentPage-1);
	html +=temp;
    }
    html = WrapTag("div",null,"listIndex",null,html);
    return html;
    
}

/*class Librarian
 *
 *handle checkout and return book request
 *
 *
 *
 *since version: 0.5
 *
 */
function Librarian()
{
}
Librarian.prototype.checkoutBook = function(ID)
{
    if(!userManager.checkLoginStatus())
    {
	Show("请先登录");
	return false;
    }
    if(getURL("api/checkout_book.php?ID="+ID)=="OK")
    {
	Show("成功借书done");
	return true;
    }
    else
    {
	Show("链接失败请联系管理员");
	return false;
    }
}
Librarian.prototype.returnBook = function(ID)
{
    if(!userManager.checkLoginStatus())
    {
	Show("请先登录");
	return false;
    }
    if(getURL("api/return_book.php?ID="+ID)=="OK")
    {
	Show("成功记录归还done");
	return true;
    }
    else
    {
	alert(ID);
	Show("归还失败,请重试fail");
	return false;
    }
}